import Vue from 'vue'
import Request from './request.js'
import helper from '@/common/helper.js' //请求拦截
import { compareVersion } from '@/common/util.js';
const http = new Request();
// const version = uni.getSystemInfoSync().version || '7.0.10'
// const platform = uni.getSystemInfoSync().platform


http.setConfig((config) => { /* 设置全局配置 */
	config.baseUrl = helper.baseUrl; //默认请求地址
	config.header = {
		'Content-Type': 'application/x-www-form-urlencoded',
	};
	return config
})

http.interceptor.request((config, cancel) => { /* 请求之前拦截器 */
	config.header = {
		token: uni.getStorageSync('token'),
        ...config.header
    }

	
	if (config.isLoading) { //加载中动画
		uni.showLoading({
			title: '加载中',
			icon: "loading",
		})
	}
		

	// console.log("请求前拦截")
	return config;
})

http.interceptor.response((response) => { /* 请求之后拦截器 */
	if (response.config.isLoading) { //关闭加载动画
		// setTimeout(function(){ 
			uni.hideLoading();
		// },100)
	}
	if(response.data.code == 2001){
		uni.clearStorageSync()
		uni.reLaunch({
			url: '/pages/login/index'
		})
		return response;
	}
	// console.log("请求后拦截")
	return response;
})

export {
	http
};
